﻿using Direct.IDAL.Manager.UserCenter;
using DirectSell.Common.Url;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Webdiyer.WebControls.Mvc;

namespace DirectSell.DAL.Manager.UserCenter
{
    /// <summary>
    /// 用户列表
    /// </summary>
    public class MemberListRepository : BaseManagerRepository, IMemberListRepository
    {
        #region 查询用户列表
        /// <summary>
        /// 查询用户列表
        /// </summary>
        /// <returns></returns>
        public async Task<PagedList<UserInforMationDetails>> QueryList(BaseCondition condition)
        {

            PageParam param = new PageParam();
            param.OrderBy = "ActivationTime Desc";
            param.TableName = "UserInforMations";
            param.PageSize = condition.pageSize;
            param.PageIndex = condition.pageIndex == 0 ? 1 : condition.pageIndex;
            StringBuilder sb = new StringBuilder();
            sb.Append(" (UserStatus=1 OR UserStatus=2) ");
            if (condition.SerachCondition != null)
            {
                sb.AppendFormat("AND (UserName LIKE '%{0}%' OR UserCode  LIKE'%{0}%' OR Phone LIKE '%{0}%') ",condition.SerachCondition.Trim());
            }

            if (!string.IsNullOrEmpty(condition.startTime))
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ActivationTime>='{0}' and ActivationTime<='{1}' ", condition.startTime, condition.endTime);
                }
                else
                {
                    sb.AppendFormat(" AND ActivationTime>='{0}' and ActivationTime<='{1}' ", condition.startTime,DateTime.Now.ToString());
                }
            }
            else
            {
                if (!string.IsNullOrEmpty(condition.endTime))
                {
                    sb.AppendFormat(" AND ActivationTime>='2010-01-01' and ActivationTime<='{1}' ", condition.endTime);
                }
            }
            param.StrWhere = sb.ToString();
            return await base.PageQuery<UserInforMationDetails>(param);
            //var list = await base.QueryAsync<UserInforMationDetails>(sb.ToString());
            //return list as IList<UserInforMationDetails>;
        }
        #endregion

        #region 冻结会员
        /// <summary>
        /// 冻结会员
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public bool FrozenUser(string userCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET UserStatus=2 ");
            sb.AppendFormat("WHERE UserCode='{0}'", userCode);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 启动会员
        /// <summary>
        /// 启用会员
        /// </summary>
        /// <param name="userCode"></param>
        /// <returns></returns>
        public bool EnableUser(string userCode)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(" UPDATE UserInforMations SET UserStatus=1 ");
            sb.AppendFormat("WHERE UserCode='{0}'", userCode);
            return base.Execute(sb.ToString());
        }
        #endregion

        #region 修改资料
        /// <summary>
        /// 修改资料
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public bool EditMessage(UserInforMationDetails info)
        {
            var sql = new StringBuilder();
            sql.AppendFormat(" UPDATE UserInforMations SET ");
            sql.AppendFormat(" UserName='{0}', ", info.UserName);
            sql.AppendFormat(" IdNumber='{0}', ", info.IdNumber);
            sql.AppendFormat(" Phone='{0}', ", info.Phone);
            sql.AppendFormat(" BankName='{0}', ", info.BankName);
            sql.AppendFormat(" BankCode='{0}', ", info.BankCode);
            sql.AppendFormat(" CardHolder='{0}' ", info.CardHolder);
            sql.AppendFormat(" WHERE UserCode = '{0}' ", info.UserCode);
            return base.Execute(sql.ToString());
        }
        #endregion

    }
}
